package com.geovis.emergency.spd.biz.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.exception.BizException;
import com.geovis.emergency.spd.biz.system.service.ISystemUserService;
import com.geovis.emergency.spd.entity.system.entity.SystemDeptUser;
import com.geovis.emergency.spd.entity.system.entity.SystemUser;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemUserSaveDTO;
import com.geovis.oauth2.sdk.dto.ApiSynSaveOrUpdateUser;
import com.geovis.oauth2.sdk.service.IGeovisSyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class PortalUserSyncServiceImpl implements IGeovisSyncService {


    private final ISystemUserService systemUserService;


    @Override
    public void syncSaveOrUpdateUser(ApiSynSaveOrUpdateUser apiSynSaveOrUpdateUser) {
        log.info("----syncSaveOrUpdateUser-----");
        long count=systemUserService.count(Wrappers.lambdaQuery(SystemUser.class)
                .eq(SystemUser::getUserName, apiSynSaveOrUpdateUser.getUsername()));
        if(count>0)
        {
            throw new BizException("用户已存在");
        }
        //角色：金宏网1458089380652266499  城阳：1458089380652266499
        //部门：金宏网1816787386017861633  城阳：1816759628906770434
        SystemUserSaveDTO saveDTO=new SystemUserSaveDTO();
        saveDTO.setUserName(apiSynSaveOrUpdateUser.getUsername())
                .setRealName(apiSynSaveOrUpdateUser.getRealName())
                .setNickName(apiSynSaveOrUpdateUser.getRealName())
                .setPhone(apiSynSaveOrUpdateUser.getPhoneNo())
                .setUserPassword("123456")
                .setDeptId("1816787386017861633")//暂时写死
                .setRoleIdList(Arrays.asList("1458089380652266499"));//金宏网1458089380652266499
        systemUserService.save(saveDTO);
        log.info("----保存用户数据-----");

    }

    @Override
    public void syncDeleteUser(String userName) {
        log.info("----syncDeleteUser-----");
        long count=systemUserService.count(Wrappers.lambdaQuery(SystemUser.class)
                .eq(SystemUser::getUserName, userName));
        if(count>0)
        {
            systemUserService.remove(Wrappers.lambdaQuery(SystemUser.class)
                    .eq(SystemUser::getUserName, userName));
            log.info("----删除用户数据-----");
        }

    }
}
